package com.flowable.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.core.domain.AjaxResult;
import com.common.core.domain.entity.SysUser;
import com.common.utils.SecurityUtils;
import com.common.utils.bean.BeanUtils;
import com.flowable.common.enums.TaskStatusEnum;
import com.flowable.controller.oa.vo.BpmOALeaveCreateReqVO;
import com.flowable.controller.oa.vo.BpmOALeavePageReqVO;
import com.flowable.controller.oa.vo.BpmOALeaveRespVO;
import com.flowable.domain.dto.BpmProcessInstanceCreateReqDTO;
import com.flowable.mapper.oa.BpmOALeaveMapper;
import com.flowable.service.IFlowInstanceService;
import com.system.domain.oa.BpmOALeaveDO;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BpmOALeaveService extends ServiceImpl<BpmOALeaveMapper, BpmOALeaveDO> {

  public static final String PROCESS_KEY = "flow_c44xhr07";
  public static final String PROCESS_ID="oa_leave:1:2504";

  @Autowired
  private IFlowInstanceService flowInstanceService;

  @Autowired
  private BpmOALeaveMapper leaveMapper;


  public   Integer createLeave( @Valid BpmOALeaveCreateReqVO createReqVO){
    SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    // 插入 OA 请假单
    long day = LocalDateTimeUtil.between(createReqVO.getStartTime(), createReqVO.getEndTime()).toDays();
    BpmOALeaveDO leave = BeanUtils.toBean(createReqVO, BpmOALeaveDO.class);
      leave.setDay(day);
      leave.setUserId(sysUser.getUserId());
      leave.setUpdater(sysUser.getUserName());
      leave.setCreator(sysUser.getUserName());
      leave.setCheckstatus(TaskStatusEnum.NOT_START.getStatus());
      LocalDateTime current = LocalDateTime.now();
      leave.setCreateTime(current);
      leave.setUpdateTime(current);
      this.save(leave);
     if(createReqVO.getSubmittype().equals("1")){
       Map<String, Object> processInstanceVariables = new HashMap<>();
       processInstanceVariables.put("day", day);
       BpmProcessInstanceCreateReqDTO bpmProcessInstanceCreateReqDTO = new BpmProcessInstanceCreateReqDTO();
       bpmProcessInstanceCreateReqDTO.setProcessDefinitionKey(PROCESS_KEY);
       bpmProcessInstanceCreateReqDTO.setTableName("bpm_oa_leave");
       bpmProcessInstanceCreateReqDTO.setVariables(processInstanceVariables);
       bpmProcessInstanceCreateReqDTO.setBusinessKey(String.valueOf(leave.getId()));
       bpmProcessInstanceCreateReqDTO.setStartUserSelectAssignees(createReqVO.getStartUserSelectAssignees());
       //flowable版本
      //AjaxResult result= flowDefinitionService.startProcessInstanceById(PROCESS_ID,processInstanceVariables);
       String processInstanceId =  flowInstanceService.createProcessInstance(sysUser.getUserId(),bpmProcessInstanceCreateReqDTO);
//    BpmOALeaveDO bpmOALeaveDO = new BpmOALeaveDO();
//    bpmOALeaveDO.setProcessInstanceId(processInstanceId);
//    bpmOALeaveDO.setCheckstatus(TaskStatusEnum.RUNNING.getStatus());
//    bpmOALeaveDO.setId(leave.getId());
//     leaveMapper.updateById(bpmOALeaveDO);
     }
      return  leave.getId();
  }

//  public BpmOALeaveDO getLeave(@RequestParam("id") Long id) {
//    return  this.baseMapper.selectById(id);
//  }


  public List<BpmOALeaveDO> selectList(BpmOALeavePageReqVO reqVO) {
    QueryWrapper<BpmOALeaveDO> queryWrapper=new QueryWrapper<>();
     // return this.baseMapper.selectList(queryWrapper);
    return leaveMapper.selectList(reqVO);
  }
}
